package com.ybfenpan.controller.member.wealth;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.ParamWage;
import com.ybfenpan.model.log.LogMoneyPay;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.ParamWageService;
import com.ybfenpan.service.log.LogElePayService;
import com.ybfenpan.service.log.LogMoneyPayService;
import com.ybfenpan.service.log.LogStorePayService;
import com.ybfenpan.taglib.annotation.Token;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
 
public class PayController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogMoneyPayService logMoneyPayService;
	@Resource
	private LogElePayService logElePayService;
	@Resource
	private LogStorePayService logStorePayService;
	@Resource
	private ParamWageService paramWageService;
	
	
	@Token(save=true)
	@RequestMapping("member/wealth/payShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		return "member/wealth/pay";
	}

	 
	
	@RequestMapping("member/wealth/ajaxGetPersonByloginName.shtml")
	@ResponseBody
	public void ajaxTuRecommendAction(HttpServletRequest request, HttpServletResponse response) {
		
		 
		Map formMap = getParameterMap(request); 
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);
		
		HashMap resultformMap = new HashMap();
		if(member==null){
			
			renderSuccess(response, false, resultformMap);
		}else{
			 
			resultformMap.put("personName", member.getPersonName());
			 
			renderSuccess(response, true, resultformMap); 
			
		}
		
	}

	
	
	@RequestMapping("member/wealth/pay.shtml")
	@ResponseBody
	@Token(remove=true)
	public void pay(HttpServletRequest request, ModelMap model, HttpServletResponse response) throws Exception{
		
		Boolean result=true;
		String msg = "";
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());

		ParamWage paramWage1011=paramWageService.getByKey(1011);
		ParamWage paramWage1012=paramWageService.getByKey(1012);

		Map formMap = getParameterMap(request);
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
		int payType = MapUtil.getIntValue(formMap, "payType");
		String content = MapUtil.getString(formMap, "content");
		Double num = MapUtil.getDoubleValue(formMap, "num");
		Member toMember = null;
    	if(!member.getTwoPassword().equals(pwd)){
    		msg = "安全密码不正确。";
    		result=false;
    	}else{
    		
    		//接收代理商验证
    		String loginName = MapUtil.getString(formMap, "loginName");
        	toMember = memberService.executMemberByLoginName(loginName);

        	if (loginName.equals(member.getLoginName())){
				msg = "同一个人之间不能相互转账！";
				result=false;
			}

        	if(toMember==null||toMember.getAccountActive().equals("F")){
        		msg = "代理商编号不存在或者未报单。";
        		result=false;
        	}else{
        		//转账金额验证
        		
        		if(num<paramWage1011.getValue()){
        			msg = "转账金额必须大于："+paramWage1011.getValue();
            		result=false;
        		}else{
        			if((num%paramWage1012.getValue())!=0){
        				msg = "转账金额必须是："+paramWage1012.getValue()+"的整数。";
                		result=false;
    				} else{
    					//转账类型
    					//转账
    					if(payType==10){
    						if(member.getMoney()<num){
    							msg = "现金积分余额不足";
    	                		result=false;
    						} 
    					}
    					  
    				}
        		}
        	}
    	}
		
    	if(result){
    		LogMoneyPay logMoneyPay = new LogMoneyPay();
			logMoneyPay.setFromMemberId(member.getId());
			logMoneyPay.setFromYu(member.getMoney());
			logMoneyPay.setToMemberId(toMember.getId());
			logMoneyPay.setToYu(toMember.getMoney());
			logMoneyPay.setNum(num);
			logMoneyPay.setContent(content);
			logMoneyPayService.savePay(logMoneyPay);
			
    	}
    	
    	HashMap resultformMap = new HashMap();
		resultformMap.put("msg", msg);
		renderSuccess(response, result, resultformMap); 
		
	}
}
